using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic.FileIO; //I ain't proud.
using System.IO;

namespace Skinner
{
    public partial class MakeTheme : Form
    {
        string inf =
@"[info]
title=MyTheme
codepage=65001
version=3.00
format=2.00
readonly=true
refcount=0

[titles]
1031=MyTheme
1036=MyTheme
1040=MyTheme
3082=MyTheme
1043=MyTheme
1046=MyTheme
1053=MyTheme
1044=MyTheme
1030=MyTheme
1035=MyTheme
1041=MyTheme
1042=MyTheme
1028=MyTheme
2052=MyTheme
1029=MyTheme
1045=MyTheme
1032=MyTheme
1038=MyTheme
1049=MyTheme
1055=MyTheme
2070=MyTheme
1025=MyTheme
1037=MyTheme
1054=MyTheme
";
        private string _targetDir;
        private StyleSheet _sheet;

        private MakeTheme()
        {
            InitializeComponent();
        }

        private HtmlDocument _doc = null;

        public MakeTheme(StyleSheet sheet, HtmlDocument doc)
        {
            InitializeComponent();
            _sheet = sheet;
            _doc = doc;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtName.Text == string.Empty)
            {
                MessageBox.Show("Please pick a name for your theme!", "Enter a name!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                _targetDir = folderBrowserDialog.SelectedPath;

                string cssText = _sheet.Sheet.cssText;

                CopyImages();

                foreach (string image in _sheet.ReplacementImages.Values)
                {
                    cssText = cssText.Replace(image, image.Substring(image.LastIndexOf(@"\") + 1));
                }

                StreamWriter outFile = new StreamWriter(_targetDir + @"\theme.css");
                outFile.Write(cssText);
                outFile.Close();
                outFile.Dispose();

                outFile = new StreamWriter(_targetDir + @"\" + txtName.Text + ".inf");
                outFile.Write(inf.Replace("MyTheme", txtName.Text));
                outFile.Close();
                outFile.Dispose();
                this.Close();
            }
        }

        private string FixBackgroundImage(string backgroundImage)
        {
            if (backgroundImage.ToLower().Substring(0, 4) == "url(")
            {
                backgroundImage = backgroundImage.Substring(4);
            }

            backgroundImage = backgroundImage.Replace(")", "");

            //if the url starts with / it it relative to the root
            if (backgroundImage.Substring(0, 1) == "/")
            {
                backgroundImage = _doc.Url.Scheme + "://" + _doc.Url.Host + backgroundImage;
            }
            //maybe it's on the local disk.
            else if (backgroundImage.Substring(1, 2) == @":\")
            {
                //do nothing else!
            }
            //otherwise it is relative to the css location.
            else
            {
                if (_sheet.Sheet.href != string.Empty)
                    backgroundImage = _doc.Url.Scheme + "://" + _doc.Url.Host + "/" + _sheet.Sheet.href.Substring(0, _sheet.Sheet.href.LastIndexOf("/")) + "/" + backgroundImage;
            }
            return backgroundImage;
        }

        private void CopyImages()
        {
            for (int i = 0; i < _sheet.Sheet.rules.length; i++)
            {
                PictureBox pic = new PictureBox();

                mshtml.IHTMLStyleSheetRule rule = _sheet.Sheet.rules.item(i);
                try
                {
                if (rule.style.backgroundImage != null && rule.style.backgroundImage.ToLower() != "none")
                {
                    string sourceImage = FixBackgroundImage(rule.style.backgroundImage);

                    string backgroundImage = rule.style.backgroundImage;
                    if (backgroundImage.ToLower().Substring(0, 4) == "url(")
                    {
                        backgroundImage = backgroundImage.Substring(4);
                    }

                    backgroundImage = backgroundImage.Replace(")", "");

                    if (backgroundImage.Contains(@"/"))
                    {
                        backgroundImage = backgroundImage.Substring(backgroundImage.LastIndexOf(@"/") + 1);
                    }
                    if (backgroundImage.Contains(@"\"))
                    {
                        backgroundImage = backgroundImage.Substring(backgroundImage.LastIndexOf(@"\") + 1);
                    }

                    if (sourceImage.Substring(1, 2) == @":\")
                    {
                        FileSystem.CopyFile(sourceImage, _targetDir + @"\" + backgroundImage, true);
                    }
                    {
                        pic.Load(sourceImage);
                        pic.Image.Save(_targetDir + @"\" + backgroundImage);
                        pic.Dispose();
                    }
                }
                }
                catch { }
            }
        }
    }
}